import java.util.Stack;

public class ReverseList {
    //递归实现
    public static void reversePrintList1(Node head) {
        if(head == null) {
            return;
        }
        reversePrintList1(head.next);
        System.out.print(head.val+" ");
    }
    //循环实现(使用栈)
    public static void reversePrintList2(Node head){
        if(head == null) {
            return;
        }
        Node cur = head;
        Stack<Node> stack = new Stack<>();
        while(cur!=null){
            stack.push(cur);
            cur = cur.next;
        }
        while(!stack.empty()){
            System.out.print(stack.pop().val+" ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addLast(1);
        singleLinkedList.addLast(2);
        singleLinkedList.addLast(3);
        singleLinkedList.addLast(4);
        singleLinkedList.myToString();
        System.out.println("逆序后");
        reversePrintList1(singleLinkedList.head);
        System.out.println();
        reversePrintList1(singleLinkedList.head);
    }
}
